<template>
  <div class="all">
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="专业名称">
        <el-input v-model="condition.name" placeholder="专业"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="getMajorInfo" icon="el-icon-a-042" style="font-size: 20px;">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="showAdd" icon="el-icon-a-07" style="font-size: 20px;">添加</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="pagination.records" border>
      <el-table-column prop="name" label="专业名称" ></el-table-column>
      <el-table-column prop="studentCount" label="学生人数" ></el-table-column>
      <el-table-column prop="examCount" label="试卷个数" ></el-table-column>
      <el-table-column fixed="right" label="操作" width="260">
        <template slot-scope="scope">
          <el-button @click="checkGrade(scope.row.id)" type="primary" icon="el-icon-a-032">编辑</el-button>
          <el-button @click="deleteById(scope.row.id)" type="danger" icon="el-icon-a-022">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagination.current"
      :page-sizes="[6, 10]"
      :page-size="pagination.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pagination.total"
      class="page">
    </el-pagination>
    <!-- 编辑对话框-->
    <el-dialog
      title="编辑专业信息"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose">
      <section class="update">
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="专业名称">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
        </el-form>
      </section>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit()">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 添加-->
    <el-dialog
      title="添加专业信息"
      :visible.sync="addVisible"
      width="30%"
      :before-close="handleClose"
    >
      <section class="update">
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="专业名称">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
        </el-form>
      </section>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取 消</el-button>
        <el-button type="primary" @click="add()">立即创建</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      pagination: {
        //分页后的专业信息
        current: 1, //当前页
        total: null, //记录条数
        size: 10, //每页条数
      },
      dialogVisible: false, //对话框
      addVisible: false, // 添加专业信息
      form: {}, //保存点击以后当前专业的信息
      condition: {
        name: "",
      }
    };
  },
  created() {
    this.getMajorInfo();
  },

  methods: {
    getMajorInfo() {
      //分页查询所有专业信息
      var name = (this.condition.name.trim() == "" ? "@" : this.condition.name);

      this.$axios(`/api/majors/${this.pagination.current}/${this.pagination.size}/${name}`).then(res => {
        this.pagination = res.data.data;
      }).catch(error => {
      });
    },
    //显示添加
    showAdd(){
      this.form = {};
      this.addVisible = true;
    },

    //改变当前记录条数
    handleSizeChange(val) {
      this.pagination.size = val;
      this.getMajorInfo();
    },
    //改变当前页码，重新发送请求
    handleCurrentChange(val) {
      this.pagination.current = val;
      this.getMajorInfo();
    },
    checkGrade(id) { //修改教师信息
      this.dialogVisible = true
      this.$axios(`/api/major/${id}`).then(res => {
        this.form = res.data.data
      })
    },
    add() {
      if (this.form.name == null || this.form.name == "") {
        this.$message({
          message: "专业名称不能为空",
          type: "error",
        });
        return;
      }

      this.$axios(`/api/examManagePaperId`).then((res) => {
        this.$axios({
          url: "/api/major",
          method: "post",
          data: {
            ...this.form,
          },
        }).then((res) => {
          if (res.data.status == 200) {
            if (res.data.data != true) {
              this.$message(res.data.message);
            } else {
              this.$message({
                message: "数据添加成功",
                type: 'success'
              })
              this.addVisible = false
              this.getMajorInfo()
            }
          }

        });
      });
    },

    deleteById(id) { //删除当前专业
      this.$confirm("确定删除当前专业吗？删除后无法恢复", "Warning", {
        confirmButtonText: '确定删除',
        cancelButtonText: '算了,留着吧',
        type: 'danger'
      }).then(() => { //确认删除
        this.$axios({
          url: `/api/major/${id}`,
          method: 'delete',
        }).then(res => {
          if (res.data.status == 200) {
            this.$message({message: res.data.message,type: 'success'});
          }else {
            this.$message({message: res.data.message,type: 'error'});
          }
          this.getMajorInfo()
        })
      }).catch(() => {

      })
    },
    submit() { //提交更改
      if (this.form.name == null || this.form.name == "") {
        this.$message({
          message: "专业名称不能为空",
          type: "error",
        });
        return;
      }

      this.$axios({
        url: '/api/major',
        method: 'put',
        data: {
          ...this.form
        }
      }).then(res => {
        if (res.data.status == 200) {
          if (res.data.data != true) {
            this.$message(res.data.message);
          } else {
            this.$message({
              message: "更新成功",
              type: 'success'
            })
            this.dialogVisible = false
            this.getMajorInfo()
          }
        }
      })
    },
    handleClose(done) { //关闭提醒
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        }).catch(_ => {
      });
    },
  }
};
</script>
<style lang="less" scoped>
.all {
  padding: 0px 40px;

  .page {
    margin-top: 20px;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .edit {
    margin-left: 20px;
  }

  .el-table tr {
    background-color: #dd5862 !important;
  }
}

.el-table .warning-row {
  background: #000 !important;
}

.el-table .success-row {
  background: #dd5862;
}
</style>
